package com.alexander.cloundexplorer.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import com.alexander.cloundexplorer.R;
import com.alexander.cloundexplorer.bean.params.MkDirParams;
import com.alexander.cloundexplorer.bean.params.User;
import com.alexander.cloundexplorer.bean.response.FileAndDirectoryInfoResponse;
import com.alexander.cloundexplorer.util.Constants;
import com.alibaba.fastjson.JSON;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * @author 14014
 * 新建文件夹
 */
public class MkDirActivity extends BaseActivity
{
    @BindView(R.id.dirName_EditText)
    EditText dirNameEditText;
    private FileAndDirectoryInfoResponse fatherDirectoryInfo;
    private User user;
    private MkDirParams mkDirParams = new MkDirParams();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mkdir);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        fatherDirectoryInfo = (FileAndDirectoryInfoResponse) intent.getSerializableExtra("data");
        user = (User) intent.getSerializableExtra("user");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_options_rename, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.sure:
                System.out.println("新建文件夹");
                String newName = dirNameEditText.getText().toString();
                if(!TextUtils.isEmpty(newName))
                {
                    mkDirParams.setAccount(user.getPhone());
                    mkDirParams.setPassword(user.getPassword());
                    mkDirParams.setFatherDirectoryId(fatherDirectoryInfo.getId());
                    mkDirParams.setDirectoryName(newName);
                    String jsonStr = JSON.toJSONString(mkDirParams);
                    OkHttpUtils
                            .post()
                            .url(Constants.IP.concat("/fileController/mkdirectory"))
                            .addParams("params", jsonStr)
                            .build()
                            .execute(new StringCallback()
                            {
                                @Override
                                public void onError(Call call, Exception e, int id)
                                {
                                    showTip(e.getMessage());
                                }
                                @Override
                                public void onResponse(String response, int id)
                                {
                                    showTip(response);
                                    if(response.contains("800"))
                                    {}
                                    else if(response.contains("801"))
                                    {}
                                }
                                @Override
                                public void onAfter(int id)
                                {
                                    super.onAfter(id);
                                    finish();
                                }
                            });
                }
                else
                {
                    showTip("请输文件夹名称");
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}
